package cn.itcast.service.system.impl;

import cn.itcast.dao.system.ModuleDao;
import cn.itcast.domain.system.Module;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.ModuleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ModuleServiceImpl implements ModuleService {
    @Autowired
    private ModuleDao moduleDao;
    @Override
    public List<Module> findAll() {
        return moduleDao.findAll();
    }

    @Override
    public void save(Module module) {
      moduleDao.save(module);
    }

    @Override
    public Module findById(String id) {
        return moduleDao.findById(id);
    }

    @Override
    public void update(Module module) {
      moduleDao.updateById(module);
    }

    @Override
    public void deleteById(String id) {
     moduleDao.deleteById(id);
    }

    @Override
    public PageInfo<Module> findByPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Module> list = moduleDao.findAll();
        return new PageInfo<Module>(list,5);
    }

    @Override
    public List<String> findModulesByRoleId(String roleid) {
        return moduleDao.findModulesByRoleId(roleid);
    }
 @Override
public List<Module> findModuleListByUser(User user) {
//        判断是哪种人 user.getDegree()
    Integer degree = user.getDegree();
    List<Module> moduleList = null;
    if(degree==0){  //SaaS管理员 ，只看属于SaaS的菜单
        moduleList = moduleDao.findByBelong(0);
    }else if(degree==1){ //企业管理员 只看属于企业的菜单
        moduleList = moduleDao.findModuleListByCompanyId(user.getCompanyId());

    }else{  //普通员工
        moduleList =  moduleDao.findByUserId(user.getId());
    }
    return moduleList;
}

    @Override
    public List<Module> findModuleListByCompanyId(String companyId) {
        return moduleDao.findModuleListByCompanyId(companyId);
    }

}
